﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyHelper;
using clf.weighing.entity;


namespace weighing
{
    public class CompanyDao : BaseDao<Company>
    {

        #region instance

        private static CompanyDao? instance;
        public static CompanyDao Instance => instance ??= new CompanyDao();

        public override string GetTableName()
        {
            return TableName.company.ToString();
        }
        #endregion

        /// <summary>
        /// 初始化时，添加公司
        /// 自动生成 公司统计的数据 
        /// </summary>
        /// <param name="company"></param>
        /// <returns></returns>
        public int InitCompany(Company company)
        {
            var res = Sqlite.Insert(company);
            return res;
        }

        /// <summary>
        /// 获致公司列表 去除使用单位
        /// </summary>
        /// <returns></returns>
        public List<Company> GetList()
        {
            String condition = CompanyField.id.ToString() + $" not in({App.InitConfig.clientId}) ";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, condition);
            var data = Sqlite.Select<Company>(sql);
            return data;
        }

        public List<Company> SelectByNameOrFirstCaseLike(String queryStr)
        {
            if (queryStr.IsNullOrEmpty()) return null;
            string condition = $" {CompanyField.name_first_case} like '%{queryStr}%' or {CompanyField.full_name} like '%{queryStr}%'";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, condition);
            var data = Sqlite.Select<Company>(sql);
            return data;
        }

        public int Delete(Company company, Boolean isTrue = false)
        {
            if (company == null)
            {
                return 0;
            }
            if (WeighingBillDao.Instance.IsCompanyHasBill(company.id))
            {
                return -1;
            }
            if (isTrue)
            {
                return Sqlite.Delete(company, isTrue);
            }
            else
            {
                String condition = CompanyField.id.ToString() + $" = {company.id}";
                String time = DateTimeHelper.getCurrentDateTime();
                String set = CompanyField.update_time.ToString() + $" = '{time}'"
                        + $", {CompanyField.is_delete} = 1 "
                        + $", {CompanyField.delete_time} = '{time}'"
                        + $", {CompanyField.update_user_name} = '{App.CurrentUser.name}'"
                        + $", {CompanyField.update_user_id} = {App.CurrentUser.id}";
                String sql = SqlBuilder.GetUpdateSql(GetTableName(), set, condition);
                return Sqlite.Delete(sql);
            }

        }

        public bool IsExist(string companyName)
        {
            String condition = CompanyField.full_name.ToString() + $" = '{companyName}'";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), "id", condition, null, null, null, 1, 0);
            var data = Sqlite.Select<Company>(sql);
            return data.IsNotNullOrEmpty();
        }

        public int Insert(long sendCompanyId, string sendCompanyName)
        {
            Company company = new Company()
            {
                id = sendCompanyId,
                fullName = sendCompanyName,
            };
            if (!IsExist(company.fullName))
            {
                company.nameFirstCase = MyHelper.StringHelper.GetFirstPinyin(company.fullName);
                company.remark = "自动添加";
                return Sqlite.Insert(company);
            }
            return 0;
        }

        public Company? FindByFullName(string fullName)
        {
            String condition = CompanyField.full_name.ToString() + $" = '{fullName}'";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), "id", condition, null, null, null, 1, 0);
            var data = Sqlite.Select<Company>(sql);
            return data.FirstOrDefault();
        }

        public int CountAll(string key = "")
        {
            string condition = $"{CompanyField.full_name} like '%{key}%' and {CompanyField.is_delete} = 0 ";
            string sql = SqlBuilder.GetSelectSql(GetTableName(), $"{CompanyField.id}", condition);
            var data = Sqlite.Select<Company>(sql);
            return data != null ? data.Count : 0;
        }

        public List<Company> SelectPage(int pageindex = 1, int size = 20, string key = "")
        {
            string condition = $"{CompanyField.full_name} like '%{key}%' and {CompanyField.is_delete} = 0 ";
            int limit = size;
            int offsert = (pageindex - 1) * limit;
            string sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, null, limit, offsert);
            var data = Sqlite.Select<Company>(sql);
            return data;
        }
    }
}
